
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>列表</title>
    {include file="include/top"}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            {include file="include/selectsearch" ctype='layui-inline' title="渠道" classname='fromID' optionHtml='<?php echo isset($fromID)?$fromID:"";?>' }
            {include file="include/selectsearch" ctype='layui-inline' title="标签筛选" classname='tips' optionHtml='<?php echo isset($tips)?$tips:"";?>' }
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="请填入..." autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
            </div>
        </div>
        <div class="layui-card-header layuiadmin-card-header-auto">
            <button type="button" class="layui-btn" id="test-upload-type1" data-imgid="addfood"><i class="layui-icon"></i>添加</button>
        </div>
        <table class="layui-table" lay-data="{height:0, url:'/gas/gasList', page:true, id:'idTest',toolbar: '#toolbarDemo'}" lay-filter="demo">
            <thead>
            <tr>
                <th lay-data="{field:'id',  sort: true, fixed: true,width:80,checkbox:true}">ID</th>
                <th lay-data="{field:'id',  sort: true, fixed: true,width:80}">ID</th>
                <th lay-data="{field:'tipsID'}">标签</th>
                <th lay-data="{field:'province'}">省份</th>
                <th lay-data="{field:'city'}">城市</th>
                <th lay-data="{field:'country'}">区县</th>
                <th lay-data="{field:'fromID'}">渠道</th>
                <th lay-data="{field:'gasID'}">三方ID</th>
                <th lay-data="{field:'name'}">名称</th>
                <th lay-data="{field:'logo_small',templet:function(d){
                    if(d.logo_small){
                        return '<img src='+d.logo_small+' width=30>';
                    }else{
                        return '';
                    }

                }}">Logo</th>
                <th lay-data="{field:'localStatus',templet:function(d){
                    if(d.localStatus==1)
                        return '正常';
                    else
                        return '暂停';
                    }
                }
                ">状态</th>
                <th lay-data="{field:'subPrice',edit:true}">让利%✎</th>
                <th lay-data="{fixed: 'right',  align:'center', toolbar: '#barDemo',width:80}"></th>
            </tr>
            </thead>
        </table>
        <script type="text/html" id="toolbarDemo">
            {include file="include/selectsearch" ctype='layui-inline' title="标签" classname='tips2' optionHtml='<?php echo isset($tips)?$tips:"";?>' }
            <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="tipsSave" data-imgid="addfood">新增标签</button>
            <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" id="tipsSave2" data-imgid="tips2">删除标签</button>
            {include file="include/selectsearch" ctype='layui-inline' title="渠道" classname='supplier' optionHtml='<?php echo isset($fromID)?$fromID:"";?>' }
            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="upGas" data-imgid="upGas">拉取油站</button>
        </script>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="more">操作<i
                    class="layui-icon layui-icon-down"></i></a>
        </script>
    </div>
</div>
{include file="include/js"}
<script>
    var pathid=0;
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table', 'dropdown'], function(){
        var table = layui.table;
        var $ = layui.$;
        var form = layui.form;
        var dropdown = layui.dropdown
        var pathid=0;
        var id=0;
        $("#test-upload-type1").click(function (){
            id=0;
            add();
        })
        $(document).on("click","#tipsSave",function () {
            editGasTip('add')
        })
        
        $(document).on("click","#upGas",function () {
            var $supplierID = $("select[name='supplier']").val()
            if ($supplierID > 0) {
                layer.confirm("是否确定操作？", function () {
                    layer.closeAll()
                    layer.msg("提交成功")
                    $.post("getGasList", {supplierID: $supplierID})
                })
            } else {
                layer.msg("选择渠道")
            }
        })
        
        $(document).on("click","#tipsSave2",function () {
            editGasTip('del')
        })
        function editGasTip($type='add'){
            var $tipsID = $("select[name='tips2']").val()
            var checkIds = []
            var selectData = table.checkStatus('idTest');
            if (selectData.data.length > 0 && $tipsID > 0) {
                selectData.data.forEach(res => {
                    checkIds.push(res.id)
                })
                layer.confirm("是否确定操作？", function () {
                    layer.closeAll()
                    layer.load()
                    $.post("tipsSaveIDS", {
                        tipsID: $tipsID,
                        type: $type,
                        checkIds: JSON.stringify(checkIds)
                    }, function (res) {
                        if (res.code == 0) {
                            layer.closeAll()
                            layer.msg(res.msg)
                        } else {
                            layer.msg(res.msg)
                        }
                    })
                })
            } else {
                layer.msg("选择标签和油站")
            }
        }
        table.on("edit(demo)",function (re){
            var $field = re.field
            var $value =  re.value
            var $id =  re.data.id
            var obj=$(this)
            if( $field == "subPrice"){
                //设置整单优惠
                $.post("gasSave", {id: $id,value:$value,field:$field}, function (res) {
                    layer.tips(res.msg,obj)
                })
            }
        })
        function add(){
            layer.open({
                type: 2
                ,title: '添加'
                ,content: 'listAdd?id='+id
                ,btn: ['确定', '取消']
                ,area: ['80%', '80%']
                ,yes: function(index, layero){
                    layer.confirm('是否确定操作?', {icon: 3, title:'提示'}, function(index){
                        //do something
                        var $data={};
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags");
                        $data.fromID = othis.find('select[name="fromID"]').val()
                        $data.status = othis.find('select[name="status"]').val()
                        $data.localStatus = othis.find('select[name="localStatus"]').val()
                        $data.gasID = othis.find('input[name="gasID"]').val()
                        $data.name = othis.find('input[name="name"]').val()
                        $data.province = othis.find('input[name="province"]').val()
                        $data.province_id = othis.find('input[name="province_id"]').val()
                        $data.city = othis.find('input[name="city"]').val()
                        $data.city_id = othis.find('input[name="city_id"]').val()
                        $data.country = othis.find('input[name="country"]').val()
                        $data.address = othis.find('input[name="address"]').val()
                        $data.lat = othis.find('input[name="lat"]').val()
                        $data.lng = othis.find('input[name="lng"]').val()
                        $data.service = othis.find('input[name="service"]').val()
                        $data.business_time = othis.find('input[name="business_time"]').val()
                        $data.star = othis.find('input[name="star"]').val()
                        $data.intro = othis.find('#LAY_layedit_2').contents().find("body").html()
                        var temArr=[]
                        var prices=[];
                        othis.find('input[name="oil_no"]').each(function (){
                            var $obj = $(this)
                            if($obj[0].checked){
                                temArr.push($obj.val())
                                var oin_name = $obj.val().replace(/^\#+|\#+$/g, '');
                                var othis2 = layero.find('iframe').contents().find("#oil_no_"+oin_name);
                                prices.push({
                                    oil_no:$obj.val()
                                    ,oil_name:oin_name
                                    ,price_yfq:othis2.find("input[name='price_yfq']").val()
                                    ,price_gun:othis2.find("input[name='price_gun']").val()
                                    ,price_official:othis2.find("input[name='price_official']").val()
                                    ,price_tuan:othis2.find("input[name='price_yfq']").val()
                                })
                            }
                        })
                        $data.oil_no = JSON.stringify(prices)
                        // $data.prices = JSON.stringify(prices)
                        $.post("gasSave?id="+id,$data,function (re) {
                            layer.msg(re.msg);
                            if(re.code == 0){
                                setTimeout(function () {
                                    location.reload();
                                },2000);
                            }
                        },'json');
                        layer.close(index);
                    });
                }
            });
        }
        function addTail(){
            layer.open({
                type: 2
                ,title: '设置出口价格'
                ,content: 'gasEdit?id='+id
                ,btn: ['确定', '取消']
                ,area: ['80%', '80%']
                ,yes: function(index, layero){
                    layer.confirm('设置后请注意利润空间', {icon: 3, title:'提示'}, function(index){
                        //do something
                        var $data=[];
                        var othis = layero.find('iframe').contents().find(".priceList");
                        othis.each(function (){
                            var re=$(this)
                            $data.push([
                                re.find("input[name='a0']").val()
                                ,re.find("input[name='a1']").val()
                                ,re.find("input[name='a2']").val()
                                ,re.find("input[name='a3']").val()
                                ,re.find("input[name='a4']").val()
                                ,re.find("input[name='a5']").val()
                                ,re.find("input[name='a6']").val()
                            ])
                        })
                        $.post("gasLocalSave?id="+id,{data:JSON.stringify($data)},function (re) {
                            layer.msg(re.msg);
                            if(re.code == 0){
                                setTimeout(function () {
                                    location.reload();
                                },2000);
                            }
                        },'json');
                        layer.close(index);
                    });
                }
            });
        }
        function guns(){
            layer.open({
                type: 2
                ,title: '添加修改枪号'
                ,content: 'gunAdd?id='+id
                ,btn: ['确定', '取消']
                ,area: ['80%', '80%']
                ,yes: function(index, layero){
                    layer.confirm('是否确定修改？', {icon: 3, title:'提示'}, function(index){
                        //do something
                        var $data=[];
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags");
                        othis.find(".layui-form-item").each(function (){
                            var re=$(this)
                            var guns = Array();
                            re.find(".gunList").each(function (){
                                $(this).find("span").each(function (){
                                    guns.push($(this).find("font").text())
                                })

                            })
                            $data.push([
                                re.find("label").text()
                            ].concat(guns))
                        })

                        $.post("gunSave?id="+id,{data:JSON.stringify($data)},function (re) {
                            layer.msg(re.msg);
                            if(re.code == 0){
                                setTimeout(function () {
                                    location.reload();
                                },2000);
                            }
                        },'json');
                        layer.close(index);
                    });
                }
            });
        }
        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('idTest', {
                where: field
            });
        });
        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(demo)', function(obj){

            var that = this
            var data = obj.data;
            if (obj.event === 'more') {
                //更多下拉菜单
                var thatData = data
                dropdown.render({
                    elem: that
                    , show: true //外部事件触发即显示
                    , data: [{
                        title: '编辑信息'
                        , id: 'gasEdit'
                    },{
                        title: '降价金额'
                        , id: 'addTail'
                    },{
                        title: '枪号管理'
                        , id: 'guns'
                    },{
                        title: '拉取信息'
                        , id: 'gasUpdate'
                    },{
                        title: '推送服务商'
                        , id: 'gasPush'
                    },{
                        title: '禁用'
                        , id: 'setDisable'
                    },
                    ]
                    , click: function (data, othis) {
                        //根据 id 做出不同操作
                        if (data.id === 'del') {

                        }else if(data.id == 'gasEdit'){
                            id = thatData.id
                            add();
                        }else if(data.id == 'addTail'){
                            id = thatData.id
                            id = thatData.id
                            addTail();
                        }else if(data.id == 'guns'){
                            id = thatData.id
                            guns();
                        }else if(data.id == 'setDisable'){
                            id = thatData.id
                            setDisable();
                        }else if(data.id == 'gasUpdate'){
                            id = thatData.id
                            layer.confirm("是否确定拉取最新？",function (){
                                layer.closeAll();
                                layer.load('更新中...')
                                $.post("getGas?id="+id,function (re) {
                                    layer.closeAll();
                                    layer.msg(re.msg);
                                },'json');

                            })

                        }else if(data.id == 'gasPush'){
                            id = thatData.id
                            layer.confirm("是否确定推送服务商？",function (){
                                layer.closeAll();
                                layer.load('推送中...')
                                $.post("gasPush?id="+id,function (re) {
                                    layer.closeAll();
                                    layer.msg(re.msg);
                                },'json');

                            })

                        }
                    }
                    , align: 'right' //右对齐弹出（v2.6.8 新增）
                    , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                });
            }
        });
    });
</script>

</body>
</html>
